package com.fishery.customization.controller;

import com.fishery.customization.entity.Theme;
import com.fishery.customization.entity.UserTheme;
import com.fishery.customization.service.ThemeService;
import com.fishery.customization.service.UserThemeService;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customization/theme")
@Api(tags = "主题管理模块")
@CrossOrigin
public class ThemeController {

	@Autowired
	private ThemeService themeService;

	@Autowired
	private UserThemeService userThemeService;

	@GetMapping
	@ApiOperation("查询所有主题")
	public Result searchAllThemes() {
		return Result.success().data(themeService.getAllThemes());
	}

	@GetMapping("/{id}")
	@ApiOperation("查询某个主题")
	public Result searchTheme(@PathVariable(name = "id", required = true)
							  @ApiParam(value = "id", required = true) String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("查询失败");
		}
		return Result.success().data(themeService.getThemeById(id));
	}


	@GetMapping("/user/{userId}")
	@ApiOperation("查询用户当前选择的主题")
	public Result searchCurrentThemeOfUser(@PathVariable(name = "userId", required = true)
										   @ApiParam(value = "userId", required = true) String userId) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("查询失败");
		}
		return Result.success().data(userThemeService.getThemeOfUserByUserId(userId));
	}


	@PostMapping("/user")
	@ApiOperation("保存或更新用户所有的主题")
	public Result addOrUpdateThemeOfUser(@RequestBody @ApiParam(value = "userTheme") UserTheme userTheme) {
		if (userTheme == null
				|| StringUtils.isEmpty(userTheme.getThemeId())
				|| StringUtils.isEmpty(userTheme.getUserId())) {
			return Result.error("操作失败");
		}
		return userThemeService.saveOrUpdateThemeOfUser(userTheme)
				? Result.success("操作成功") : Result.error("操作失败");
	}

	@GetMapping("/{size}/{current}")
	@ApiOperation("分页查询所有主题")

	public Result searchPageOfAllThemes(@PathVariable(name = "current", required = true) @ApiParam(value = "页码") int current,
										@PathVariable(name = "size", required = true) @ApiParam(value = "尺寸") int size) {
		return Result.success().data(themeService.pageAllThemes(current, size));
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除主题")
	public Result deleteTheme(@PathVariable(name = "id", required = true)
							  @ApiParam(value = "id", required = true) String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		return themeService.removeThemeById(id) ? Result.success("删除成功") : Result.error("删除失败");
	}

	@DeleteMapping("/batch")
	@ApiOperation("批量删除主题")
	public Result deleteThemeWithBatch(@RequestBody @ApiParam(value = "ids") List<String> ids) {
		if (ids == null || ids.size() == 0) {
			return Result.error("批量删除失败");
		}
		return themeService.removeThemeWithBatch(ids) ?
				Result.success("批量删除成功") : Result.error("批量删除失败");
	}

	@PostMapping
	@ApiOperation("添加主题")
	public Result addTheme(@RequestBody @ApiParam(value = "theme") Theme theme) {
		return themeService.saveTheme(theme) ? Result.success("添加成功") : Result.error("添加失败");
	}

	@PostMapping("/batch")
	@ApiOperation("批量添加主题")
	public Result addThemeWithBatch(@RequestBody @ApiParam(value = "themes") List<Theme> themes) {
		return themeService.saveThemeWithBatch(themes) ? Result.success("添加成功") : Result.error("添加失败");
	}

	@PutMapping
	@ApiOperation("修改主题")
	public Result updateTheme(@RequestBody @ApiParam(value = "theme") Theme theme) {
		if (theme == null || StringUtils.isEmpty(theme.getId())) {
			return Result.error("修改失败");
		}
		return themeService.updateThemeById(theme) ? Result.success("修改成功") : Result.error("修改失败");
	}

}
